﻿using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using sgs_Entity;
using sgs_Common;
using System.Collections.Generic;
using System;
using Microsoft.VisualBasic.PowerPacks;
using Debugger.MyControls;

namespace Debugger
{
    #region 页面枚举
    /// <summary>
    /// 图片状态（选中Selected、未选中UnSelected）
    /// </summary>
    enum PictureStatus
    {
        /// <summary>
        /// 选中
        /// </summary>
        Selected,

        /// <summary>
        /// 未选中
        /// </summary>
        UnSelected
    }
    #endregion

    public partial class Match1v1 : Form
    {
        #region 页面常量
        /// <summary>
        /// 卡牌宽度
        /// </summary>
        private const int PicWidth = 200;
        /// <summary>
        /// 卡牌高度
        /// </summary>
        private const int PicHeight = 289;
        /// <summary>
        /// 卡牌突出高度
        /// </summary>
        private const int StandOut = 30;
        /// <summary>
        /// 卡牌显示位置X
        /// </summary>
        private const int initialX = 50;
        /// <summary>
        /// 卡牌显示位置Y（下方）
        /// </summary>
        private const int initialY_D = 500;
        /// <summary>
        /// 卡牌显示位置Y（上方）
        /// </summary>
        private const int initialY_U = 50;
        /// <summary>
        /// 卡牌突出显示位置Y
        /// </summary>
        private const int SelectedLocationY_D = initialY_D - 30;
        /// <summary>
        /// 卡牌最大长度
        /// </summary>
        private const int MaxLength = 1200;
        #endregion

        /// <summary>
        /// 页面生成事件，执行初始化代码
        /// </summary>
        public Match1v1()
        {
            InitializeComponent();

            // 初始化卡牌
            InitialCards();
            // 初始化玩家
            InitialPlayers();
            // 初始化窗体控件
            InitialControls();


        }


        /// <summary>
        /// 初始化玩家
        /// </summary>
        private void InitialPlayers()
        {
            GlobalVariables.ListPlayer = new List<Player>();

            // 生成我的玩家，白板武将
            GlobalVariables.MyPlayer = new Player();
            GlobalVariables.MyPlayer.ID = 0;
            GlobalVariables.MyPlayer.Name = "我方";
            GlobalVariables.MyPlayer.Warrior = new Warrior(WarriorName.NA, 4, Kingdom.NA, WarriorSet.NA, new SkillName[0]);
            GlobalVariables.ListPlayer.Add(GlobalVariables.MyPlayer);

            // 生成敌对玩家，白板武将
            GlobalVariables.OppositePlayer1 = new Player();
            GlobalVariables.OppositePlayer1.ID = 0;
            GlobalVariables.OppositePlayer1.Name = "对方";
            GlobalVariables.OppositePlayer1.Warrior = new Warrior(WarriorName.NA, 4, Kingdom.NA, WarriorSet.NA, new SkillName[0]);
            GlobalVariables.ListPlayer.Add(GlobalVariables.OppositePlayer1);
        }

        /// <summary>
        /// 初始化卡牌
        /// </summary>
        private void InitialCards()
        {
            // 资源管理器、牌堆
            ResourceManager rm = new ResourceManager("Debugger.Properties.Resources", Assembly.GetExecutingAssembly());
            Cards Cards = Cards.CreateAllCards();

            // 洗牌，存储卡牌对应图片
            GlobalVariables.DicImage = new Dictionary<int, Image>();
            GlobalVariables.ListCard = Cards.ShuffleCards(Cards);
            GlobalVariables.ListCard.ForEach(delegate(Card card)
            {
                GlobalVariables.DicImage.Add(card.ID, (Image)rm.GetObject("_" + card.ID.ToString()));
            });
        }

        /// <summary>
        /// 初始化窗体控件
        /// </summary>
        private void InitialControls()
        {
            // 画布（覆盖整个页面）
            ShapeContainer scContainer = new ShapeContainer();
            scContainer.Size = this.Size;
            scContainer.Location = this.Location;
            this.Controls.Add(scContainer);

            // 设置我方卡牌的框
            RectangleShape rsMyCard = new RectangleShape();
            rsMyCard.Size = new Size(MaxLength + 10, PicHeight + 10);
            rsMyCard.Location = new Point(initialX - 5, initialY_D - 5);
            scContainer.Shapes.Add(rsMyCard);

            // 设置我方玩家的人物框
            ucPlayer MyPlayerBox = new ucPlayer(GlobalVariables.MyPlayer);
            MyPlayerBox.Location = new Point(initialX + MaxLength + 150, initialY_D - 5);
            this.Controls.Add(MyPlayerBox);

            // 设置对方玩家的人物框
            ucPlayer OppositePlayerBox = new ucPlayer(GlobalVariables.OppositePlayer1);
            OppositePlayerBox.Location = new Point(initialX, initialY_U - 5);
            this.Controls.Add(OppositePlayerBox);
        }







        /// <summary>
        /// 摸牌方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawCard_Click(object sender, System.EventArgs e)
        {
            // 新建图片控件
            PictureBox pb = new PictureBox();

            #region 生成卡牌图片控件
            // 摸出第一张未使用状态的牌
            foreach (Card card in GlobalVariables.ListCard)
            {
                if (card.CardStatus == CardStatus.Free)
                {
                    // 给图片赋值——ID，图片，大小，鼠标移上样式，备注（是否选中）
                    pb.Name = card.ID.ToString();
                    pb.Image = GlobalVariables.DicImage[card.ID];
                    pb.Size = new Size(PicWidth, PicHeight);
                    pb.Cursor = Cursors.Hand;
                    pb.Tag = PictureStatus.UnSelected;
                    // 卡牌属性赋值——状态、所属玩家
                    card.CardStatus = CardStatus.InHand;
                    card.BelongToPlayer = null;
                    // 鼠标移入事件
                    pb.MouseEnter += new EventHandler(delegate(object o, EventArgs ea)
                    {
                        // 卡牌突出
                        PictureBox temppb = o as PictureBox;
                        if (temppb.Tag != null && (PictureStatus)temppb.Tag == PictureStatus.UnSelected)
                        {
                            temppb.Location = new Point(temppb.Location.X, SelectedLocationY_D);
                            temppb.BringToFront();
                        }
                    });
                    // 鼠标移出事件
                    pb.MouseLeave += new EventHandler(delegate(object o, EventArgs ea)
                    {
                        // 卡牌缩回
                        PictureBox temppb = o as PictureBox;
                        if ((PictureStatus)temppb.Tag == PictureStatus.UnSelected)
                        {
                            temppb.Location = new Point(temppb.Location.X, initialY_D);
                            temppb.BringToFront();
                        }
                        ResetPicture();
                    });
                    // 鼠标点击事件
                    pb.MouseUp += new MouseEventHandler(delegate(object o, MouseEventArgs ea)
                    {
                        PictureBox temppb = o as PictureBox;
                        if ((PictureStatus)temppb.Tag == PictureStatus.UnSelected)
                        {
                            // 清除已选牌
                            GlobalVariables.ListCard.ForEach(delegate(Card entity)
                            {
                                if (entity.CardStatus == CardStatus.InHand && entity.BelongToPlayer == null)
                                {
                                    Control control = this.Controls[entity.ID.ToString()];
                                    if (control.Tag != null && (PictureStatus)control.Tag == PictureStatus.Selected)
                                    {
                                        control.Tag = PictureStatus.UnSelected;
                                        control.Location = new Point(control.Location.X, initialY_D);
                                    }
                                }
                            });
                            temppb.Tag = PictureStatus.Selected;
                            temppb.Location = new Point(temppb.Location.X, SelectedLocationY_D);
                            temppb.BringToFront();
                        }
                        else
                        {
                            temppb.Tag = PictureStatus.UnSelected;
                            temppb.Location = new Point(temppb.Location.X, initialY_D);
                            temppb.BringToFront();
                        }
                        ResetPicture();
                    });

                    break;
                }
            }
            #endregion

            #region 设置卡牌图片控件Location
            // 卡牌展示位置
            List<Card> CardsInHand = GlobalVariables.ListCard.FindAll(delegate(Card card) { return card.CardStatus == CardStatus.InHand && card.BelongToPlayer == null; });
            // 第一张卡牌
            if (CardsInHand.Count == 1)
            {
                this.Controls.Add(pb);
                pb.Location = new Point(initialX, initialY_D);
            }
            // 小于7张卡牌（卡牌不重叠）
            else if (CardsInHand.Count <= 6)
            {
                this.Controls.Add(pb);
                pb.Location = new Point(initialX + (CardsInHand.Count - 1) * pb.Size.Width, initialY_D);
            }
            // 大于、等于7张卡牌（卡牌重叠）
            else
            {
                this.Controls.Add(pb);
                int interval = (MaxLength - pb.Width) / (CardsInHand.Count - 1);
                for (int i = 0; i < CardsInHand.Count; i++)
                {
                    Card card = CardsInHand[i];
                    Control control = this.Controls[card.ID.ToString()];
                    control.Location = new Point(initialX + i * interval, initialY_D);
                    control.BringToFront();
                }
            }
            #endregion
        }
        /// <summary>
        /// 卡牌复位，突出显示已选卡牌
        /// </summary>
        private void ResetPicture()
        {
            Control SelectedCard = null;
            GlobalVariables.ListCard.ForEach(delegate(Card entity)
            {
                if (entity.CardStatus == CardStatus.InHand && entity.BelongToPlayer == null)
                {
                    Control control = this.Controls[entity.ID.ToString()];
                    control.BringToFront();
                    if (control.Tag != null && (PictureStatus)control.Tag == PictureStatus.Selected)
                    {
                        SelectedCard = control;
                    }
                }
            });
            if (SelectedCard != null)
                SelectedCard.BringToFront();
        }
    }
}
